/*
 * Copyright (C) Jochen Hiller, Bernd Kolb.
 * 
 * Licensed under Apache 2.0.
 * See http://www.apache.org/licenses/LICENSE-2.0
 */
package demo.geolocation;

import java.util.List;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class GeoLocationActivity extends Activity {

	private LocationManager locationManager;
	private TextView output;
	private String bestLocationProvider;
	private LocationListener myTracker = new MyLocationTracker();
	private MyGeoDecoder geodecoder;

	private static final String[] A = { "n/a", "fine", "coarse" };
	private static final String[] S = { "out of service",
			"temporarily unavailable", "available" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		output = (TextView) findViewById(R.id.output);

		display("Location Providers available:");
		List<String> providers = locationManager.getAllProviders();
		for (String provider : providers) {
			LocationProvider info = locationManager.getProvider(provider);
			display("name=" + info.getName() + ", accuracy="
					+ A[info.getAccuracy()]);
		}

		chooseBestLocationProvider();

		geodecoder = new MyGeoDecoder(this.getApplicationContext());

		display("\nLocations (starting with last known):");
		Location location = locationManager
				.getLastKnownLocation(bestLocationProvider);
		displayLocation(location);

	}

	@Override
	protected void onResume() {
		super.onResume();
		// Start updates (doc recommends delay >= 60000 ms)
		// 1 means notify when location change more than 1 meter
		locationManager.requestLocationUpdates(bestLocationProvider, 15000, 1,
				myTracker);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Stop updates to save power while app paused
		locationManager.removeUpdates(myTracker);
	}

	// private methods

	private void chooseBestLocationProvider() {
		Criteria criteria = new Criteria();
		// as it may called from location listener, this may be called
		// asynchronously, so sync it
		synchronized (locationManager) {
			bestLocationProvider = locationManager.getBestProvider(criteria,
					true);
		}
		display("\nBest location provider is: " + bestLocationProvider);
	}

	private void display(String string) {
		output.append(string + "\n");
	}

	private void displayLocation(Location location) {
		if (location == null) {
			display("\nLocation[unknown]");
		} else {
			// display("\n" + location.toString());
			display("\nlatitude=" + location.getLatitude() + ", longitude="
					+ location.getLongitude());
			display(geodecoder.getNameForLocation(location));
		}
	}

	// inner classes

	private class MyLocationTracker implements LocationListener {

		public void onLocationChanged(Location location) {
			displayLocation(location);
		}

		public void onProviderDisabled(String provider) {
			display("\nProvider disabled: " + provider);
			chooseBestLocationProvider();
		}

		public void onProviderEnabled(String provider) {
			display("\nProvider enabled: " + provider);
			chooseBestLocationProvider();
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			display("\nProvider status changed: " + provider + ", status="
					+ S[status] + ", extras=" + extras);
		}

	}
}